
export const Cart = {
    state:{
        cartList:[],  //购物车列表
        stateAll:false //全选状态
    },
    mutations:{
        //添加购物车
        ADD_CART(state,product){
            state.cartList.push(product)
        },
        //更改全选状态
        SET_STATE_ALL(state,all){
            //设置全选状态
            state.stateAll = all
            // 所有单选状态
 state.cartList.forEach(item=>item.state=state.stateAll)
        },
        //设置单选状态
        SET_SINGLE_STATE(state){
            // 设置所单选状
            state.stateAll = state.cartList.every(item=>item.state)
        },
        //删除
        DELETE_PRODUCT(state,id){
            let index = state.cartList.findIndex(item=>item.id==id)
            state.cartList.splice(index,1)
        }
    },
    actions:{
        addCart({commit},product){
            commit('ADD_CART',product)
        },
        setStateAll({commit},all){
            commit('SET_STATE_ALL',all)
        },
        setSingleState({commit}){
            commit('SET_SINGLE_STATE')
        },
        deleteProduct({commit},id){
            commit('DELETE_PRODUCT',id)
        }
    }
}